<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Campaignuser extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['controller'] = 'campaignuser';
        $this->load->model('Mcampaignuser');
        $this->load->model('Mindex');
    }
    
    
    
	public function index()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2,3));
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(2,3);
        $data = $this->Mcampaignuser->getCampaignUser($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$this->session->userdata('admin_id'));
        $config['base_url'] = base_url($this->uri->segment(1).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data;
        
        $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function checkEndTime(){
        $time_start = $this->input->post('time_start');
        $time_end = $this->input->post('time_end');
        if(strtotime($time_start) < strtotime($time_end)) {
            return true;
        } else {
            $this->form_validation->set_message('checkEndTime', 'Thời gian kết thúc phải lớn hơn thời gian bắt đầu!');
            return false;
        }
    }
    public function checkStartTime(){
        $time_start = $this->input->post('time_start');
        $time = date('Y/m/d H:i');
        if(strtotime($time) < strtotime($time_start)) {
            return true;
        } else {
            $this->form_validation->set_message('checkStartTime', 'Thời gian bắt đầu phải lớn hơn thời gian hiện tại!');
            return false;
        }
    }
	public function addcampaignuser(){
        
        $this->checkPermission($this->session->userdata('level'), array(3));
		$this->load->library('form_validation');
        $this->form_validation->set_rules('type_ads', 'Kiểu quảng cáo', 'required');
		$this->form_validation->set_rules('address_id', 'Địa điểm', 'required');
		$this->form_validation->set_rules('title', 'Tên chiến dịch', 'required');
		$this->form_validation->set_rules('time_start', 'Thời gian bắt đầu', 'required|callback_checkStartTime');
        $this->form_validation->set_rules('time_end', 'Thời gian kết thúc', 'required|callback_checkEndTime');
        $this->form_validation->set_rules('cost', 'Số tiền bạn dùng để đặt quảng cáo', 'required');
        $this->form_validation->set_rules('oneview','Kiểu hiển thị','required');
			//$this->form_validation->set_rules('note', 'Yêu cầu khác', 'required');
        $this->data['dataListShop'] = $this->Mcampaignuser->dataListShop();
		if($this->form_validation->run() === TRUE)
        {
            $type_ads = $this->getParamInt('type_ads');
			$address_id = $this->getParamArray('address_id');
            $title = $this->getParamString('title');	
			$time_start = $this->getParamString('time_start');
			$time_end = $this->getParamString('time_end');
			$cost = $this->getParamInt('cost');
            $note = $this->getParamString('note');
            $oneview = $this->getParamInt('oneview');
            
            if ( $cost > $this->session->userdata('money') ){
                die('<meta charset="utf-8"><script>alert("Bạn không đủ tiền trong tài khoản");window.location.href="'.$this->data['baseurl'].'add-campaign-user.html";</script>');
            }
            
			$run = $this->Mcampaignuser->addCampaignUser($type_ads,$address_id,$title,$time_start,$time_end,$note,$cost,$oneview);
			if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'campaign-user.html";</script>');
            }
        }
        $this->data['action'] = 'addcampaignuser';
        $this->load->view('temp/layout', $this->data);
	}
	
	
	public function editcampaignuser(){
        $this->checkPermission($this->session->userdata('level'), array(3));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('address_id', 'Địa điểm', 'required');
		$this->form_validation->set_rules('title', 'Tên chiến dịch', 'required');
		$this->form_validation->set_rules('time_start', 'Thời gian bắt đầu', 'required|callback_checkStartTime');
        $this->form_validation->set_rules('time_end', 'Thời gian kết thúc', 'required|callback_checkEndTime');
		$id = $this->getParamUri(2,1);
		$this->data['cid'] = $id;
		$this->data['data'] = $this->Mcampaignuser->getCampaignuserInfo($id);
	if ( $this->data['data']['data']->active != 0 || $this->data['data']['data']->id_customers != $this->session->userdata('id_user') ){ // 2-Duyêt thành công; 4-Đã xóa
			redirect('campaign-user');
		}
        $this->data['dataListShop'] = $this->Mcampaignuser->dataListShop();
		if($this->form_validation->run() === TRUE)
        {
			$address_id = $this->getParamArray('address_id');
            $title = $this->getParamString('title');	
			$time_start = $this->getParamString('time_start');
			$time_end = $this->getParamString('time_end');
			$note = $this->getParamString('note');

			$run = $this->Mcampaignuser->editCampaignUser($address_id,$title,$time_start,$time_end,$note,$id);
			if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'campaign-user.html";</script>');
            }
        }
        $this->data['action'] = 'editcampaignuser';
        $this->load->view('temp/layout', $this->data);
	}
	
	public function deletecampaignuser()
    {
        $this->checkPermission($this->session->userdata('level'), array(3));
        $id = $this->getParamUri(2,1);
        $run = $this->Mcampaignuser->deleteCampaignUserDb($id);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'campaign-user.html";</script>');
        }
    }
	
	
	public function campaignuserdetail(){
	    $this->checkPermission($this->session->userdata('level'), array(1,2,3));
		$id = $this->getParamUri(2,1);
		$this->data['cid'] = $id;
		$this->data['data'] = $this->Mcampaignuser->getCampaignuserDetail($id);
        //die('-'.$this->data['data']['data']->active);
		if ( $this->data['data']['data']->active == 0 || $this->data['data']['data']->id_customers != $this->session->userdata('id_user')  ){ 
			redirect('campaign-user');
		}
        $this->data['dataListShop'] = $this->Mcampaignuser->dataListShop();
        $this->data['action'] = 'campaignuserdetail';
        $this->load->view('temp/layout', $this->data);
	}
    
    public function placeuser()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2,3));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['place'] = $this->Mcampaignuser->getPlace();
        //echo "<pre>";
        //die(print_r($this->data['place']));
        $this->data['action'] = 'placeuser';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function report()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,3)); // admin + user
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mcampaignuser->getCountEndUsers($this->session->userdata('id_user'));
        $campaignReport = $this->Mcampaignuser->getCampaignReport();
        $this->data['campaignReport'] = $campaignReport['data']; 
        $this->data['arr_'] = $campaignReport['arr_'];
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        $this->data['action'] = 'report';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function ajaxgetcampaignreport(){
        $last_id = $this->getParamInt('last_id');
        $campaignReport = $this->Mcampaignuser->ajaxGetCampaignReport($last_id);
        die(json_encode($campaignReport['data']));
    }
    
    public function statistic()
    {
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $time_select = $this->getParamString('time_select');
		//die('cid:'.$this->data['c_id'].'--pid:'.$this->data['p_id']);
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        
		//echo '<pre>';die(print_r($this->data['nameplace']));
		
		
		$data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        //echo '<pre>';die(print_r($data_time));
		
		
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['type'] = $this->getParamInt('type');
		
        $this->data['chart_ab'] = $this->Mcampaignuser->getChartAb($this->data['c_id'], $this->data['time'], $this->data['type'], $this->session->userdata('id_user'),$this->data['p_id']);
        
		//echo '<pre>';die(print_r($this->data['chart_ab']));
		
		
		
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 25;
        $this->data['page'] = $this->getParamUri(2,3);
        $this->data['per_page'] = $config['per_page'];
        if(!in_array($this->data['c_id'], array(null, '', '0')) || !in_array($this->data['time_select'], array(null, '', '0')))
        {
            $config['paramhack'] = '?id='.$this->data['c_id'].'&type='.$this->data['type'].'&time='.$this->data['time'].'&time_select='.$this->data['time_select'].'#page';
        }
        
        $offer = (($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0);
        $id_user = $this->session->userdata('id_user');
        $data = $this->Mcampaignuser->getLogAccess($config['per_page'],$offer,$id_user,$this->data['c_id'],$this->data['time']);
   
        
        $config['base_url'] = base_url('page_ajax_detail/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $config['ellipsize'] = TRUE;
        $config['stt_reduced']  = 10;
        $config['reduced']  = 10;
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        
        $_data = $this->Mcampaignuser->getCount($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id'],$this->data['p_id']);
        //echo "<pre>";die(print_r($_data));
        $this->data['browser'] = $_data->browser;
        $this->data['browser_ver'] = $_data->browser_ver;
        $this->data['device_name'] = $_data->device_name;
        $this->data['os_group'] = $_data->os_group;
        
        
        $this->data['show_time'] = $this->Mcampaignuser->getCountTime($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id'], 20, 0);
        
             
             
        $this->data['getmail'] = $this->Mcampaignuser->getmail($this->data['p_id'], $this->data['c_id'], $this->data['time']);
        $this->data['getfacebook'] = $this->Mcampaignuser->getfacebook($this->data['p_id'], $this->data['c_id'], $this->data['time']);
		$this->data['getmobile'] = $this->Mcampaignuser->getmobile($this->data['p_id'], $this->data['c_id'], $this->data['time']);
        
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
       //echo "<pre>";die(print_r($this->data));
        //$this->data['data'] = $this->Mcontact->getdetail($id);
        
        $this->data['selectType'] = $this->selectType($this->getParamInt('type'));
        $this->data['action'] = 'statistic';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function selectType($id)
    {
        $arr = (object)array('Tất cả','Trình duyệt','Thiết bị','Hệ điều hành');
        $html  = '';
        foreach($arr AS $el=>$le)
        {
            if($id == $el)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$el.'"'.$selected.'>'.$le.'</option>';
        }
        return $html;
    }
    
    
    public function userexportreportplace()
    {
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExport($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/userexportreportplace', $this->data);
    }
    
    public function gettimeaccess()
    {
        
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        
        $time_select = $this->getParamString('time_select');
            
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['show_time'] = $this->Mcampaignuser->getCountTime($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id'], 20, $this->getParamInt('dem'));
        if(in_array($this->data['show_time'], array(null, '', '0')))
        {
            die('201');
        }
        //echo $this->getParamInt('dem');
        //die(print_r($this->data['show_time']));
        $this->load->view('temp/statics/gettimeaccess', $this->data);
    }
    
    public function exportreportemail()
    {
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExportEmail($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/exportreportemail', $this->data);
    }
    
    public function exportreportfacebook()
    {
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExportFacebook($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/exportreportfacebook', $this->data);
    }
    
    public function exportreportmobile()
    {
        $this->data['c_id_encode'] = $this->getParamString('c_id');
        $this->data['p_id_encode'] = $this->getParamString('p_id');
        $this->data['c_id'] = $this->encrypt->decode(base64_decode($this->data['c_id_encode']), 'statistic');
        $this->data['p_id'] = $this->encrypt->decode(base64_decode($this->data['p_id_encode']), 'statistic');
        $this->data['nameplace'] = $this->Mcampaignuser->getNamePlace($this->data['p_id'], $this->data['c_id']);
        $time_select = $this->getParamString('time_select');
        
        $data_time = $this->Mcampaignuser->getTimeCampaign($this->data['c_id'],$this->session->userdata('id_user'));
        
        $this->data['title'] = $data_time->title;
        if(!in_array($time_select, array(null, '', '0')))
        {
            $this->data['time_select'] = $time_select;
            $this->data['time'] = $this->getParamString('time');
        }else{
            $this->data['time_select'] = $data_time->time_select;
            $this->data['time'] = $data_time->time;
        }
        
        $this->data['data'] = $this->Mcampaignuser->getExportMobile($this->session->userdata('id_user'),$this->data['time'],$this->data['c_id']);

        $this->load->view('temp/campaignuser/exportreportmobile', $this->data);
    }
    
    public function popupreporting()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2,3));
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mcampaignuser->getCountEndUsers($this->session->userdata('id_user'));
        $campaignReport = $this->Mcampaignuser->getReportExport();
        $this->data['campaignReport'] = $campaignReport['data'];
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;

        $this->data['action'] = 'reportexport';
        $this->load->view('temp/campaignuser/reportexport', $this->data);
    }
   
    public function ajaxgetcostshop(){
        $type = $this->getParamInt('type');
        die(json_encode($this->Mcampaignuser->ajaxGetCostShop($type)));
    }
    
    public function ajaxpopupreporting()
    {
        $this->checkPermission($this->session->userdata('level'), array(1,2,3));
		$this->checkAdmin($this->session->userdata('id_user'));
        $_data = $this->Mcampaignuser->getCountEndUsers($this->session->userdata('id_user'));
        
        $times = $this->getParamString('times');
        $times_start = $this->getParamString('times_start');
        $times_end = $this->getParamString('times_end');
        $campaignReport = $this->Mcampaignuser->getAjaxReportExport($times,$times_start,$times_end);
        
        $this->data['campaignReport'] = $campaignReport['data'];
        
        $this->data['browser'] = $_data->browser;
        $this->data['device'] = $_data->device;
        $this->data['os'] = $_data->os;
        
        $this->data['action'] = 'ajaxreportexport';
        $this->load->view('temp/campaignuser/ajaxreportexport', $this->data);
    }
    
    public function historytrade(){
        $this->checkPermission($this->session->userdata('level'), array(1,2,3));
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(2,3);
        $data = $this->Mcampaignuser->getHistoryTrade($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0),$this->session->userdata('admin_id'));
        $config['base_url'] = base_url($this->uri->segment(1).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data;
        
        $this->data['action'] = 'historytrade';
        $this->load->view('temp/layout', $this->data);
    }
    
}